from django.shortcuts import render,redirect
from django.http import HttpResponse
from common.models import Users
from django.core.urlresolvers import reverse
from datetime import datetime
from django.core.paginator import Paginator
#浏览
def index(request,pindex=1):
	if pindex == '' or int(pindex)<=0:
		pindex="1"
	pindex=int(pindex)
	#获取users信息
	_list=Users.objects.all()
	pageData=Paginator(_list,6)
	list=pageData.page(pindex)
	plist=pageData.page_range
	context={'userList':list,'plist':plist,'pageCount':pageData.num_pages,'pindex':pindex}
	return render(request,'myadmin/users/index.html',context)

#添加页面
def add(request):
	return render(request,'myadmin/users/add.html')

#添加操作
def insert(request):
	try:
		ob = Users()
		ob.username = request.POST['username']
		ob.name = request.POST['name']
		#md5加密
		import hashlib
		m = hashlib.md5() 
		m.update(bytes(request.POST['password'],encoding="utf8"))
		ob.password = m.hexdigest()
		ob.sex = request.POST['sex']
		ob.address = request.POST['address']
		ob.code = request.POST['code']
		ob.phone = request.POST['phone']
		ob.email = request.POST['email']
		ob.state = 1
		ob.addtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
		ob.save()
		pindex =1
		context = {"info":"添加成功！"}	
		#成功后转发至列表页面
		return redirect(reverse('myadmin_users_index',args=(pindex,)))
		#return render(request,'myadmin/info.html',content)	
	except Exception as e:
		content={'info':'添加失败：：'+str(e)}
		return render(request,'myadmin/info.html',content)

#删除
def delete(request,uid):
	try:
		userObj=Users.objects.get(id=uid)
		userObj.delete()
		pindex =1
		return redirect(reverse('myadmin_users_index',args=(pindex,)))
	except Exception as e:
		content={'info':'删除失败：：'+str(e)}
		return render(request,'myadmin/info.html',content)

#编辑页面
def edit(request,uid):
	try:
		userObj=Users.objects.get(id=uid)
		content={"user":userObj}
		return render(request,'myadmin/users/edit.html',content)
	except Exception as e:
		content={'info':'查询用户详情失败：：'+str(e)}
		return render(request,'myadmin/info.html',content)
#修改密码页面
def showPassword(request,uid):
	content={"userId":uid}
	return render(request,'myadmin/users/updatePwd.html',content)

#修改密码
def updatePwd(request):
	try:
		uid=request.POST.get('uid','')
		ob=Users.objects.get(id=uid)
		pwd=request.POST.get('pwd','')
		c_pwd=request.POST.get('c_pwd','')
		#校验密码
		if pwd !='' and c_pwd !='' and pwd == c_pwd:
			import hashlib
			m = hashlib.md5() 
			m.update(bytes(request.POST['pwd'],encoding="utf8"))
			ob.password=m.hexdigest()
			ob.save()
			content={'info':'更改密码成功!!!'}
		else:
	   		content={'info':'两次输入密码不一致!!!'}
	except Exception as e:
		content={'info':'更改密码操作失败：：'+str(e)}
	finally:
		return render(request,'myadmin/info.html',content)

	


#编辑更改操作
def update(request,uid):
	try:
		ob = Users.objects.get(id=uid)
		ob.name = request.POST['name']
		ob.sex = request.POST['sex']
		ob.address = request.POST['address']
		ob.code = request.POST['code']
		ob.phone = request.POST['phone']
		ob.email = request.POST['email']
		ob.state = request.POST['state']
		ob.save()
		pindex =1
		#成功后转发至列表页面
		return redirect(reverse('myadmin_users_index',args=(pindex,)))
	except Exception as e:
		content={'info':'更改信息失败：：'+str(e)}
		return render(request,'myadmin/info.html',content)

